#!/usr/bin/env python

import ast
import sys
from pysimplesoap.client import SoapClient


class WsMgmt(object):

    def __init__(self, username, password):
        self.client = SoapClient(wsdl='https://api.dc1.computing.cloud.it/WsEndUser/v2.5/WsEndUser.svc?wsdl',
                                 location="https://api.dc1.computing.cloud.it/WsEndUser/v2.5/WsEndUser.svc",
                                 action='https://api.computing.cloud.it/WsEndUser/IWsEndUser',
                                 namespace='https://api.computing.cloud.it/WsEndUser',
                                 trace=False
                                 )
        # set manualy namespace, due to a pysimplesoap bug
        self.client.namespace = 'https://api.computing.cloud.it/WsEndUser'
        self.client['wsse:Security'] = {
            'wsse:UsernameToken': {
                'wsse:Username': username,
                'wsse:Password': password,
            }
        }

    def __str__(self):
        return self.client.namespace

    def get_hypervisor(self):
        self.hypervisors = self.client.GetHypervisors()
        return self.hypervisors

    def create_vm(self):
        req = dict(AdministratorPassword='Fuffa123',
                   CPUQuantity=1,
                   Name='ProvaAPI',
                   OSTemplateId=5,
                   RAMQuantity=2,
                   TemplateID=5,
                   VirtualDisks=[{'VirtualDiskDetails': {
                       'size': 50,
                   }}]
                   )
        res = ws.client.SetEnqueueServerCreation(req)
        return res


if __name__ == '__main__':
    ws = WsMgmt('ARU-2856', 'quWxa323-q')
    print(ws.create_vm())
    ws_dict = ws.get_hypervisor()
    sys.exit(0)
    for elem in ws_dict['GetHypervisorsResult']['Value']:
        for i_elem in elem['Hypervisor']['Templates']:
            if 'Windows 2008 R2 64bit' in i_elem['TemplateDetails']['Description']:
                print i_elem['TemplateDetails']
        sys.exit(0)
